package _interview150;

/**
 * 42. 接雨水
 */
public class No42 {
    public int trap(int[] height) {
        int n = height.length;
        int ans = 0;
        int pre = height[0], suf = height[n - 1];
        int left = 1, right = n - 2;
        while (left <= right) {
            if (pre < suf) {
                if (height[left] > pre) pre = height[left];
                else ans += pre - height[left];
                left++;
            } else {
                if (height[right] > suf) suf = height[right];
                else ans += suf - height[right];
                right--;
            }
        }
        return ans;
    }
}
